<#include "license.ftl">
<@license/>
<#assign object = doc.object>
package ${object.@package}.gwt.model.fields;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;

import redora.client.constants.RedoraConstants;
import redora.client.Fields;
import redora.client.ui.ListPopupCell.PopupListColumn;
import redora.client.ui.LazyCell.LazyColumn;
import redora.client.util.Field;
import redora.client.util.Field.Type;
import ${object.@package}.gwt.constants.*;
import ${object.@package}.gwt.model.*;

import static com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat.DATE_SHORT;

/**
* For each pojo in the model a PojoFields class is created containing all the
* attributes of that pojo. You can use ${object.@name}Fields anywhere you want
* to reference attributes of ${object.@name} in your GWT application.<br>
* The attributes are wrapped in a {@link Field} object providing alle necessary
* meta data on this attribute including i18n'ed labels.
* @see Field
* @author Redora (www.redora.net)
*/
public class ${object.@name}Fields implements Fields {

    final static Constants constants = GWT.create(Constants.class);
    final static RedoraConstants redoraConstants = GWT.create(RedoraConstants.class);
    final static GlobalEnumConstants enumConstants = GWT.create(GlobalEnumConstants.class);

    public final static Field id = new Field("id", redoraConstants.id_table(), redoraConstants.id_form(), "${object.@name}.id", Type.Long, true, null, null);

    public final static Field creationDate = new Field("creationDate", redoraConstants.creationDate_table(), redoraConstants.creationDate_form(), "${object.@name}.creationDate", Type.Datetime, true, null, new TextColumn<${object.@name}>() {
        @Override
        public String getValue(${object.@name} pojo) {
            return DateTimeFormat.getFormat(DATE_SHORT).format(pojo.getCreationDate());
        }
    });

    public final static Field updateDate = new Field("updateDate", redoraConstants.updateDate_table(), redoraConstants.updateDate_form(), "${object.@name}.updateDate", Type.Datetime, false, null, new TextColumn<${object.@name}>() {
        @Override
        public String getValue(${object.@name} pojo) {
            return pojo.getUpdateDate() == null ? null : DateTimeFormat.getFormat(DATE_SHORT).format(pojo.getUpdateDate());
        }
    });

<#list object.attributes?children as att>
    <#if att?node_type == "element" && att?node_name != "object">
        <#if att?node_name == "string">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}", constants.${object.@name?uncap_first}_${att.@fieldName}_table(),
            constants.${object.@name?uncap_first}_${att.@fieldName}_form(), "${object.@name}.${att.@fieldName}", Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>, ${att.@maxlength}, new TextColumn<${object.@name}>() {
        @Override
        public String getValue(${object.@name} pojo) {
            return pojo.get${att.@fieldName?cap_first}();
        }
    });
        <#elseif att?node_name == "enum">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}"
            <#if att.@scope == "global">
        , enumConstants.${att.@class?uncap_first}_table()
        , enumConstants.${att.@class?uncap_first}_form()
            <#else>
        , constants.${object.@name?uncap_first}_${att.@fieldName}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName}_form()
            </#if>
        , "${object.@name}.${att.@fieldName}", Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>, new TextColumn<${object.@name}>() {
        @Override
        public String getValue(${object.@name} pojo) {
            return pojo.get${att.@fieldName?cap_first}() == null ? null : pojo.get${att.@fieldName?cap_first}().displayTable();
        }
    });
        <#elseif att?node_name == "set">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}"
        , constants.${object.@name?uncap_first}_${att.@fieldName}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName}_form()
        , "${object.@name}.${att.@fieldName}", Type.Set, false
        , <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>
        , new PopupListColumn<${object.@name}>(${att.@class}.DefaultFinder.FindBy${att.@myName?cap_first}Id.ordinal(), ${att.@class}.class) {
            @Override
            public String getValue(${object.@name} pojo) {
                return null; //unused
            }
        });
        <#elseif att?node_name == "long" && att.@parentClass[0]??>
            <#assign fieldNameLength = att.@fieldName?length />
    public final static Field ${att.@fieldName} = new Field(
        "${att.@fieldName}"
        , constants.${object.@name?uncap_first}_${att.@fieldName?substring(0,fieldNameLength-2)}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName?substring(0,fieldNameLength-2)}_form()
        , "${object.@name}.${att.@fieldName}", Type.Object
        , <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>
        , <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>
        , new LazyColumn<${object.@name}>(${att.@parentClass}.class) {
            @Override
            public Long getValue(${object.@name} pojo) {
                return pojo.get${att.@fieldName?cap_first}();
            }
        });
        <#elseif att?node_name == "long" || att?node_name == "integer">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}", constants.${object.@name?uncap_first}_${att.@fieldName}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName}_form(), "${object.@name}.${att.@fieldName}"
        , Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>
        , new Column<${object.@name}, Number>(new NumberCell(NumberFormat.getFormat("#"))) {
            @Override
            public Number getValue(${object.@name} pojo) {
                return pojo.get${att.@fieldName?cap_first}();
            }
        });
        <#elseif att?node_name == "double">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}", constants.${object.@name?uncap_first}_${att.@fieldName}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName}_form(), "${object.@name}.${att.@fieldName}"
        , Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>
        , new Column<${object.@name}, Number>(new NumberCell()) {
            @Override
            public Number getValue(${object.@name} pojo) {
                return pojo.get${att.@fieldName?cap_first}();
            }
        });
        <#elseif att?node_name == "boolean">
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}", constants.${object.@name?uncap_first}_${att.@fieldName}_table()
        , constants.${object.@name?uncap_first}_${att.@fieldName}_form(), "${object.@name}.${att.@fieldName}"
        , Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>
        , new Column<${object.@name}, Boolean>(new CheckboxCell()) {  //TODO change in 2.2 to enabled = false
            @Override
            public Boolean getValue(${object.@name} pojo) {
                return pojo.get${att.@fieldName?cap_first}();
            }
        });
        <#else>
//    public static class ${att.@fieldName?cap_first}TextColumn extends TextColumn<${object.@name}> {
//        @Override
//        public String getValue(${object.@name} pojo) {
//            return pojo.get${att.@fieldName?cap_first}() == null ? null : pojo.get${att.@fieldName?cap_first}().toString();
//        }
//    }
    public final static Field ${att.@fieldName} = new Field("${att.@fieldName}", constants.${object.@name?uncap_first}_${att.@fieldName}_table(),
            constants.${object.@name?uncap_first}_${att.@fieldName}_form(), "${object.@name}.${att.@fieldName}", Type.${att?node_name?cap_first}, <#if att.@notnull[0]?? && att.@notnull == 'true'>true<#else>false</#if>, <#if att.help[0]??>constants.${object.@name?uncap_first}_${att.@name}_help()<#else>null</#if>, null);

        </#if>
    </#if>
</#list>

    public static final Field[] fields = {id, creationDate, updateDate
<#list object.attributes?children as att>
    <#if att?node_type == "element" && att?node_name != "object">
        , ${att.@fieldName}
    </#if>
</#list>
    };

    public static final Field[] tableFields = {
<#assign comma = "">
<#list object.tableScope?children as att>
    <#if att?node_type == "element" && att?node_name != "object">
        ${comma}${att.@fieldName}<#assign comma = ", ">
    </#if>
</#list>
    };

    public static final Field[] setFields = {
<#assign comma = "">
<#list object.attributes.set as att>
        ${comma}${att.@fieldName}<#assign comma = ", ">
</#list>
    };

    public static final Field[] tableAndSetFields = {
<#assign comma = "">
<#list object.tableScope?children as att>
    <#if att?node_type == "element" && att?node_name != "object">
        ${comma}${att.@fieldName}<#assign comma = ", ">
    </#if>
</#list>
<#list object.attributes.set as att>
        ${comma}${att.@fieldName}<#assign comma = ", ">
</#list>
    };

    public static final Field[] listFields = {
<#assign comma = "">
<#if object.listScope[0]??>
<#list object.listScope?children as att>
    <#if att?node_type == "element" && att?node_name != "object">
        ${comma}${att.@fieldName}<#assign comma = ", ">
    </#if>
</#list>
</#if>
    };

    @Override
    public Field[] fields() {
        return fields;
    }
    @Override
    public Field[] setFields() {
        return setFields;
    }
    @Override
    public Field[] tableFields() {
        return tableFields;
    }
    @Override
    public Field[] tableAndSetFields() {
        return tableAndSetFields;
    }
    @Override
    public Field[] listFields() {
        return listFields;
    }
}